//
//  BRHomeView.m
//  BrezzeSaaS
//
//  Created by IOS on 2023/3/15.
//  Copyright © 2023 Brezze GWG. All rights reserved.
//

#import "BRHomeView.h"


@interface BRHomeView ()



//@property (nonatomic,strong) UIView *stationBGView;


@end

@implementation BRHomeView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setUI];
    }
    return self;
}


#pragma mark - func --
- (void)setOrderDto:(BROrderDetailModel *)orderDto {
    _orderDto = orderDto;
    
    [self.orderView start:orderDto.createTime.integerValue];
    self.orderView.estimatedStr = [NSString stringWithFormat:@"%.2f",orderDto.money.floatValue];
    
}
- (void)toOpenMenu {
    
    if (self.toClickBlock) {
        self.toClickBlock(0);
    }
}

- (void)toFAQ {
    if (self.toClickBlock) {
        self.toClickBlock(1);
    }
}

- (void)toStationDetail {
    
    if (self.toClickBlock) {
        self.toClickBlock(2);
    }
}


- (void)toClickTimer {

    [self showView:1];
}


- (void)hasOrder:(BOOL)has {
    
    self.timeTopView.hidden = !has;
    
//    [self br_hiddenAll:(self.isOpenOrder)];
    if (has) return;
    if (self.isOpenOrder) {
        [self showView:1];
    }
    [self.orderView cancleTimer];
}

- (void)br_hiddenShowCard {
    
    self.orderView.isOpen = NO;
    self.isOpenOrder = NO;
    
    [self orderViewShow:YES];
    
    [self br_refreshViewH];
    self.shadowView.alpha = 0;
}

- (void)hidden {
    [self br_hiddenShowCard];
}


- (void)showView:(NSInteger)type {
    
    if (self.orderView.isOpen) {
        self.orderView.isOpen = NO;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.28 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self orderViewShow:YES]; //当前是展开 所以填YES
        });
        
    }else {
        // 展开
        self.orderView.hidden = NO;
        [self orderViewShow:self.isOpenOrder];
    }
    self.isOpenOrder = !self.isOpenOrder;
    
    CGFloat h = self.isOpenOrder ? kScreenHeight :kSafeAreaTopHeight;

    self.mj_h = h;
    self.shadowView.alpha = self.isOpenOrder ? 1 :0;
//    [self br_hiddenAll:(self.isOpenOrder)];
}

- (void)updateHight:(BOOL)isOpen {

    
//    self.mj_h = kScreenHeight;
//    [self br_hiddenAll:(self.isOpenOrder)];
}

- (void)br_refreshViewH {
    CGFloat yInScreen = KStatusBarHeight + 60 + 10;
//    yInScreen = yInScreen + 62;
    NSLog(@"当前View在屏幕上的Y坐标为:-%.f", yInScreen);
//    [self bringSubviewToFront:self.bannerView];
    self.mj_h = yInScreen;
}

- (void)viewChangY:(BOOL)open by:(UIView *)view y:(CGFloat)y opBlock:(BRVoidBlock)opBlock close:(BRVoidBlock)closeBlock {

    if (!open) {
        
        [UIView animateWithDuration:0.25 animations:^{
            view.transform = CGAffineTransformMakeTranslation(0, y);
        } completion:^(BOOL finished) {
            opBlock();
        }];
        return;
    }
    [UIView animateWithDuration:0.25 animations:^{
        view.transform = CGAffineTransformMakeRotation(0);
    } completion:^(BOOL finished) {
        closeBlock();
    }];
    
}

- (void)orderViewShow:(BOOL)show {
    
    [self viewChangY:show by:self.orderView y:144 opBlock:^{
        
    } close:^{
        self.orderView.hidden = YES;
    }];
}

- (void)stationViewShow:(BOOL)show {
//    [self viewChangY:show by:self.stationBGView y:172 opBlock:^{
//
//    } close:^{
//
//    }];
}

- (void)setTimerStr:(NSString *)timerStr {
    _timerStr = timerStr;
    self.timeTopView.timerStr = timerStr;
}

#pragma mark - UI --
- (void)setUI {
    self.backgroundColor = UIColor.clearColor;
    
    UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
//    if (@available(iOS 13,*)) {
//        effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
//    }
    
    if ([BRThemeManage isHasDark]) {
        effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    }
    UIVisualEffectView *shadowView = [[UIVisualEffectView alloc] initWithEffect:effect];
    shadowView.alpha = 0;
    [self addSubview:shadowView];
    [shadowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hidden)];
    [shadowView addGestureRecognizer:tap];
    _shadowView = shadowView;
    
    
    UIView *topBG = [[UIView alloc] init];
    topBG.backgroundColor = UIColor.clearColor;
    [self addSubview:topBG];
    [topBG mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(60 + KStatusBarHeight);
        make.top.left.right.offset(0);
    }];
    UIView *top = [[UIView alloc] init];
    top.backgroundColor = UIColor.qd_backgroundColor;
    [topBG addSubview:top];
    [top mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
    
    [top layoutIfNeeded];
    CGRect topFrame = CGRectMake(0, 0, kScreenWidth, 60 + KStatusBarHeight);
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:topFrame byRoundingCorners: UIRectCornerBottomRight | UIRectCornerBottomLeft cornerRadii:CGSizeMake(33, 33)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];

    // 设置大小
    maskLayer.frame = topFrame;
    // 设置图形样子
    maskLayer.path = maskPath.CGPath;
    top.layer.mask = maskLayer;
    
    UIImageView *img = [[UIImageView alloc] initWithImage:UIImage.qd_homeMenuImg];
    [top addSubview:img];
    [img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(32);
        make.left.offset(16);
        make.bottom.offset(-14);
    }];
    
    UIControl *menuBtn = [[UIControl alloc] init];
    [top addSubview:menuBtn];
    [menuBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(64);
        make.height.mas_equalTo(60);
        make.left.bottom.offset(0);
    }];
    [menuBtn addTarget:self action:@selector(toOpenMenu) forControlEvents:UIControlEventTouchUpInside];
    
    QMUIButton *faqBtn = [[QMUIButton alloc] init];
    [faqBtn setBackgroundColor:UIColor.qd_tintColor4];
    [faqBtn setImage:UIImageMake(@"home_ic_faq") forState:UIControlStateNormal];
    faqBtn.layer.cornerRadius = 16;
    [top addSubview:faqBtn];
    [faqBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(32);
        make.right.offset(-16);
        make.bottom.offset(-14);
    }];
    
    [faqBtn addTarget:self action:@selector(toFAQ) forControlEvents:UIControlEventTouchUpInside];
    
    [top addSubview:self.timeTopView];
    [self.timeTopView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(-14);
        make.right.equalTo(faqBtn.mas_left).offset(-12);
        make.width.mas_equalTo(128);
        make.height.mas_equalTo(32);
    }];
    UIControl *timeBtn = [[UIControl alloc] init];
    [self.timeTopView addSubview:timeBtn];
    [timeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
    [timeBtn addTarget:self action:@selector(toClickTimer) forControlEvents:UIControlEventTouchUpInside];
    
    topBG.layer.shadowColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.06].CGColor;
    topBG.layer.shadowOffset = CGSizeMake(0,6);
    topBG.layer.shadowOpacity = 1;
    // ========== ↑ 顶部主视图   =================
    self.orderView.hidden = YES;
    [self insertSubview:self.orderView atIndex:1];
    
//    [self insertSubview:self.stationBGView atIndex:0];
//    self.stationView.userInteractionEnabled = YES;
//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toStationDetail)];
//    [self.stationView addGestureRecognizer:tap];
    
//    UIView *keepOut = [UIView new];//用于遮挡订单视图or网点视图误触
//    keepOut.backgroundColor = UIColor.clearColor;
//    [self insertSubview:keepOut atIndex:2];
//    [keepOut mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.offset(0);
//        make.height.mas_equalTo(20);
//        make.top.equalTo(top.mas_bottom).offset(-5);
//    }];
    
//    [self insertSubview:self.bannerView atIndex:0];
//    [self.bannerView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.offset(16);
////        make.height.mas_equalTo(60);
//        make.width.mas_equalTo(kScreenWidth - 32);
//        make.top.equalTo(self.stationBGView.mas_bottom).offset(12);
//    }];
//    
//    [self insertSubview:self.ruleView atIndex:0];
//    [self.ruleView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.offset(0);
////        make.height.mas_equalTo(60);
//        make.width.mas_equalTo(kScreenWidth);
//        make.top.equalTo(self.bannerView.mas_bottom).offset(0);
//    }];
//    
//    [self insertSubview:self.unpayView atIndex:0];
//    [self.unpayView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.offset(16);
//        make.height.mas_equalTo(60);
//        make.width.mas_equalTo(kScreenWidth - 32);
//        make.top.equalTo(self.bannerView.mas_bottom).offset(0);
//    }];
    
    WS(weakSelf);
    self.orderView.timerBlock = ^(NSString *timeStr) {
        weakSelf.timeTopView.timerStr = timeStr;
    };
    
    self.orderView.openBlock = ^(BOOL open) {
        [weakSelf updateHight:open];
    };
    
//    self.layer.shadowColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.06].CGColor;
//    self.layer.shadowOffset = CGSizeMake(0,6);
//    self.layer.shadowOpacity = 1;
}

- (BRHomeTimeView *)timeTopView {
    
    if (!_timeTopView) {
        _timeTopView = [[BRHomeTimeView alloc] init];
        _timeTopView.hidden = YES;
    }
    return _timeTopView;
}

- (BRHomeTopView *)orderView {
    if (!_orderView) {
        _orderView = [[BRHomeTopView alloc] initWithFrame:CGRectMake(0,50 + KStatusBarHeight - 144, kScreenWidth, 162)];
        [_orderView hidden];
    }
    
    return _orderView;
}

//- (UIView *)stationBGView {
//    if (!_stationBGView) {
//        _stationBGView = [[UIView alloc] initWithFrame:CGRectMake(0,50 + KStatusBarHeight - 172, kScreenWidth, 182)];
//        _stationBGView.backgroundColor = UIColor.clearColor;
//
//        //用于遮罩顶部圆角
//        UIView *toView = [[UIView alloc] initWithFrame:CGRectMake(16, 0, kScreenWidth - 32, 50)];
//        toView.backgroundColor = UIColor.qd_backgroundColor2;
//        [_stationBGView addSubview:toView];
//
//        [_stationBGView addSubview:self.stationView];
//
//    }
//
//    return _stationBGView;
//}

//- (BRStatilListTableViewCell *)stationView {
//    
//    if (_stationView == nil) {
//        _stationView = [[[NSBundle mainBundle] loadNibNamed:@"BRStatilListTableViewCell" owner:nil options:nil] lastObject];
//        _stationView.backgroundColor = UIColorClear;
//        _stationView.contentView.backgroundColor = UIColorClear;
//        _stationView.frame = CGRectMake(0, 10, kScreenWidth, 172);
//        _stationView.contentBGView.layer.cornerRadius = 24;
//    }
//    
//    return _stationView;
//}

//- (BRUnpayView *)unpayView {
//    if (_unpayView == nil) {
//        _unpayView = [[BRUnpayView alloc] initWithFrame:CGRectMake(16, 18 + KStatusBarHeight, kScreenWidth - 32, 60)];
//        [_unpayView hidden];
//    }
//
//    return _unpayView;
//}
//
//- (BRBannerView *)bannerView {
//
//    if (_bannerView == nil) {
//        _bannerView = [[BRBannerView alloc] init];
////        _bannerView.
//    }
//    return _bannerView;
//}

//- (BRRuleView *)ruleView {
//
//    if (_ruleView == nil) {
//        _ruleView = [[BRRuleView alloc] init];
//        _ruleView.isOpen = NO;
//    }
//    return _ruleView;
//}

@end

#pragma mark - timer --
@implementation BRHomeTimeView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setUI];
    }
    return self;
}

- (void)setTimerStr:(NSString *)timerStr {
    _timerStr = timerStr;
    self.timeL.text = timerStr;
}

#pragma mark - UI --
- (void)setUI {
    self.backgroundColor = UIColor.clearColor;
//    self.layer.cornerRadius = 16;
//    self.layer.masksToBounds = YES;
    UIImageView *bgImg = [[UIImageView alloc] initWithImage:UIImageMake(@"rent_chargingBG")];
    [self addSubview:bgImg];
    [bgImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
    UIImageView *img = [[UIImageView alloc] initWithImage:UIImageMake(@"rent_charging")];
    [self addSubview:img];
    [img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(24);
        make.left.offset(4);
        make.centerY.offset(0);
    }];
    
    QMUILabel *timeL = [BRToolsView br_label:@"00:00:00.00" textColor:UIColor.whiteColor font:[UIFont fontR:14]];
    
    [self addSubview:timeL];
    [timeL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(img.mas_right).offset(4);
//        make.right.offset(-4);
        make.centerY.offset(0);
    }];
    
    _timeL = timeL;
}

@end
